﻿using System.IO;
using System.Text;

namespace Pagination.Tokenizer
{
    public class StreamTokenizer : ITokenizer
    {
        private readonly StreamReader reader;

        public StreamTokenizer(Stream stream)
        {
            reader = new StreamReader(stream, Encoding.UTF8, true, 65535);
        }

        public string Next()
        {
            SkipWhiteSpaces();
            if (reader.Peek() == -1)
                return null;
            var token = "";
            while (reader.Peek() != -1)
            {
                var chr = (char)reader.Peek();
                if (IsWhiteSpace(chr))
                    break;
                token += chr;
                reader.Read();
            }
            return token == "" ? null : token;
        }

        private void SkipWhiteSpaces()
        {
            while (reader.Peek() != -1)
            {
                var chr = (char)reader.Peek();
                if (!IsWhiteSpace(chr))
                    break;
                reader.Read();
            }
        }

        private static bool IsWhiteSpace(char chr)
        {
            return char.IsWhiteSpace(chr);
        }

        public void Dispose()
        {
            reader.Close();
        }
    }
}
